﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medical.Business;
using System.Data;
using Medical.Entity;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

/// <summary>
/// 首页
/// </summary>
public partial class Default : WebPage
{
    protected int nCount = 1;
    public DataTable dtDiseaseBanner;
    //public DataTable dtDiseaseFont;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitData();
        }
    }

    /// <summary>
    /// 页面数据初始化
    /// </summary>
    protected void InitData()
    {
        BisArticle bisArticle = new BisArticle();
        BisBanner bisBanner = new BisBanner();
        //医院新闻
        string strInfo = string.Empty;
        DataTable dtNews = bisArticle.GetTopArticle(7, ArticelTypeEnum.News, 51);
        if (dtNews.Rows.Count > 0)
        {
            ltlNewsTitle.Text = "<a href=\"" + Until.GetUrlName(dtNews.Rows[0]["PSortUrlName"], dtNews.Rows[0]["SortUrlName"], dtNews.Rows[0]["ID"]) + "\" title=\"" + dtNews.Rows[0]["Name"].ToString() + "\" target=\"_blank\">" + Server.HtmlEncode(dtNews.Rows[0]["Name"].ToString()) + "</a>";
            strInfo = Common.NoHTML(dtNews.Rows[0]["Info"].ToString());
            ltlNewsInfo.Text = (strInfo.Length > 32 ? strInfo.Substring(0, 32) : strInfo) + "…" +
                             "<a href=\"" + Until.GetUrlName(dtNews.Rows[0]["PSortUrlName"], dtNews.Rows[0]["SortUrlName"], dtNews.Rows[0]["ID"]) + "\" title=\"" + dtNews.Rows[0]["Name"].ToString() + "\" target=\"_blank\">[查看详情]</a>";
            dtNews.Rows.RemoveAt(0);
            dtNews.AcceptChanges();
        }
        rptNews.DataSource = dtNews;
        rptNews.DataBind();
        dtNews.Dispose();

        //媒体报道
        DataTable dtNews1 = bisArticle.GetTopArticle(7, ArticelTypeEnum.News, 52);
        if (dtNews1.Rows.Count > 0)
        {
            ltlNewsTitle1.Text = "<a href=\"" + Until.GetUrlName(dtNews1.Rows[0]["PSortUrlName"], dtNews1.Rows[0]["SortUrlName"], dtNews1.Rows[0]["ID"]) + "\" title=\"" + dtNews1.Rows[0]["Name"].ToString() + "\" target=\"_blank\">" + Server.HtmlEncode(dtNews1.Rows[0]["Name"].ToString()) + "</a>";
            strInfo = Common.NoHTML(dtNews1.Rows[0]["Info"].ToString());
            ltlNewsInfo1.Text = (strInfo.Length > 32 ? strInfo.Substring(0, 32) : strInfo) + "…" +
                             "<a href=\"" + Until.GetUrlName(dtNews1.Rows[0]["PSortUrlName"], dtNews1.Rows[0]["SortUrlName"], dtNews1.Rows[0]["ID"]) + "\" title=\"" + dtNews1.Rows[0]["Name"].ToString() + "\" target=\"_blank\">[详情]</a>";
            dtNews1.Rows.RemoveAt(0);
            dtNews1.AcceptChanges();
        }
        rptNews1.DataSource = dtNews1;
        rptNews1.DataBind();
        dtNews1.Dispose();

        //最新文章
        //DataTable dtArticle = bisArticle.GetTopArticle(6,ArticelTypeEnum.Disease);
        //rptArticle.DataSource = dtArticle;
        //rptArticle.DataBind();

        ////最新文章
        //DataTable dtGuide = bisArticle.GetTopArticle(3, ArticelTypeEnum.Guide);
        //rptGuide.DataSource = dtArticle;
        //rptGuide.DataBind();

        //专家团队
        DataTable dtDoctor = bisArticle.GetTopArticle(6, ArticelTypeEnum.Expert, 53);
        rptDoctor.DataSource = dtDoctor;
        rptDoctor.DataBind();

        //专家团队
        //DataTable dtDoctor1 = bisArticle.GetTopArticle(3, ArticelTypeEnum.Expert, 53);
        //rptDoctor1.DataSource = dtDoctor1;
        //rptDoctor1.DataBind();

        //简介
        Article aboutInfo = bisArticle.GetArticle(ArticelTypeEnum.About, 1);
        if (aboutInfo != null)
        {
            strInfo = Common.NoHTML(aboutInfo.Info);
            ltlAbout.Text = (strInfo.Length > 35 ? strInfo.Substring(0, 35) : strInfo) + "…";
            ltlAbout1.Text = (strInfo.Length > 50 ? strInfo.Substring(0, 50) : strInfo) + "…";
        }
        //首页荣誉
        DataTable dtHonor = bisBanner.GetList(AdverTypeEnum.Honor);
        rptHonor.DataSource = dtHonor;
        rptHonor.DataBind();

        //疾病图片
        dtDiseaseBanner = bisBanner.GetListBySort(AdverTypeEnum.Disease);

        //疾病
        DataTable dtDisease = Common.GetDiseaseSort();
        DataRow[] drDiseaseList = dtDisease.Select("ParentID=0");
        DataTable dtNewDisease = dtDisease.Clone();
        strInfo = string.Empty;
        for (int i = 0; i < dtDisease.Rows.Count; i++)
        {
            if (i == 4)
                break;
            strInfo += "<a href=\"/" + dtDisease.Rows[i]["PageUrlName"].ToString() + "/\" target=\"_blank\" title=\"" + dtDisease.Rows[i]["Name"].ToString() + "\">" + Server.HtmlEncode(dtDisease.Rows[i]["Name"].ToString()) + "</a> ";
            DataRow drTem = dtNewDisease.NewRow();
            foreach (DataColumn column in dtDisease.Columns)
            {
                drTem[column.ColumnName] = dtDisease.Rows[i][column.ColumnName];
            }
            dtNewDisease.Rows.Add(drTem);
        }
        rptDisease.DataSource = dtNewDisease;
        rptDisease.DataBind();


        //典型病例
        DataTable dtCase = bisArticle.GetTopArticle(5, ArticelTypeEnum.Case, 57);
        if (dtCase.Rows.Count > 0)
        {
            ltlCase1.Text = "<a href=\"" + Until.GetUrlName(dtCase.Rows[0]["PSortUrlName"], dtCase.Rows[0]["SortUrlName"], dtCase.Rows[0]["ID"]) + "\" title=\"" + dtCase.Rows[0]["Name"].ToString() + "\" target=\"_blank\">" + Server.HtmlEncode(dtCase.Rows[0]["Name"].ToString()) + "</a>";
            //  ltlCase1.Text = ltlCase.Text;
            dtCase.Rows.RemoveAt(0);
            dtCase.AcceptChanges();
        }
        //rptCase.DataSource = dtCase;
        //rptCase.DataBind();
        rptCase1.DataSource = dtCase;
        rptCase1.DataBind();

        //首页广告
        DataTable dtBanner = bisBanner.GetList(AdverTypeEnum.HomeBanner);
        string strBanner = string.Empty;
        if (dtBanner != null && dtBanner.Rows.Count > 0)
        {
            if (!string.IsNullOrEmpty(dtBanner.Rows[0]["Url"].ToString()))
            {
                strBanner = "<a href=\"" + dtBanner.Rows[0]["Url"].ToString() + "\" title=\"" + dtBanner.Rows[0]["Name"].ToString() + "\" target=\"_blank\">";
            }
            strBanner += "<img src=\"/" + WebConfigInfo.FilePlace + "d/" + dtBanner.Rows[0]["ImgPlace"].ToString() + "\" alt=\"" + dtBanner.Rows[0]["Name"].ToString() + "\" />";
            if (!string.IsNullOrEmpty(dtBanner.Rows[0]["Url"].ToString()))
            {
                strBanner += "</a>";
            }
        }
        ltlHomeBanner.Text = strBanner;

        //DataTable dtBanner1 = bisBanner.GetList(AdverTypeEnum.HomeBanner1);
        //string strBanner1 = string.Empty;
        //if (dtBanner1 != null && dtBanner1.Rows.Count > 0)
        //{
        //    if (!string.IsNullOrEmpty(dtBanner1.Rows[0]["Url"].ToString()))
        //    {
        //        strBanner1 = "<a href=\"" + dtBanner1.Rows[0]["Url"].ToString() + "\" title=\"" + dtBanner1.Rows[0]["Name"].ToString() + "\" target=\"_blank\">";
        //    }
        //    strBanner1 += "<img src=\"/" + WebConfigInfo.FilePlace + "d/" + dtBanner1.Rows[0]["ImgPlace"].ToString() + "\" alt=\"" + dtBanner1.Rows[0]["Name"].ToString() + "\" />";
        //    if (!string.IsNullOrEmpty(dtBanner1.Rows[0]["Url"].ToString()))
        //    {
        //        strBanner1 += "</a>";
        //    }
        //}
        //ltlHomeBanner1.Text = strBanner1;

        DataTable dtzjadver = new BisSortInfo().GetSortList(54, ArticelTypeEnum.Health);
        rptzjadver.DataSource = dtzjadver;
        rptzjadver.DataBind();

        //DataTable dtzjadver1 = bisBanner.GetList(AdverTypeEnum.Health1);
        //rptzjadver1.DataSource = dtzjadver1;
        //rptzjadver1.DataBind();


        //专家答疑
        DataTable dtFaq = new BisFeedBack().GetTopList(7, 0);
        if (dtFaq.Rows.Count > 0)
        {
            ltlFaqTitle.Text = "<a href=\"/faq/" + dtFaq.Rows[0]["SortUrlName"].ToString() + "/" + dtFaq.Rows[0]["ID"].ToString() + ".html\" title=\"" + dtFaq.Rows[0]["Title"].ToString() + "\" target=\"_blank\">" + Server.HtmlEncode(dtFaq.Rows[0]["Title"].ToString()) + "</a>";
            strInfo = Common.NoHTML(dtFaq.Rows[0]["Info"].ToString());
            ltlFaqInfo.Text = (strInfo.Length > 28 ? strInfo.Substring(0, 28) : strInfo) + "…" +
                             "<a href=\"/faq/" + dtFaq.Rows[0]["SortUrlName"].ToString() + "/" + dtFaq.Rows[0]["ID"].ToString() + ".html\" title=\"" + dtFaq.Rows[0]["Title"].ToString() + "\" target=\"_blank\">[详情]</a>";
            dtFaq.Rows.RemoveAt(0);
            dtFaq.AcceptChanges();
        }
        rptFaq.DataSource = dtFaq;
        rptFaq.DataBind();
        dtFaq.Dispose();
        //友情链接
        DataTable dtLink = new BisLinkInfo().GetAllLink();
        DataRow[] drLinkList1 = dtLink.Select("LinkType=3");
        DataTable dtNewLink1 = dtLink.Clone();
        foreach (DataRow drLink in drLinkList1)
        {
            DataRow drTem = dtNewLink1.NewRow();
            foreach (DataColumn column in dtLink.Columns)
            {
                drTem[column.ColumnName] = drLink[column.ColumnName];
            }
            dtNewLink1.Rows.Add(drTem);
        }
        rptLink1.DataSource = dtNewLink1;
        rptLink1.DataBind();
        
        nCount = 1;
        //领先技术
        DataTable dtTechnology = bisArticle.GetArticleList(ArticelTypeEnum.Technology, 1);
        rptTechnology.DataSource = dtTechnology;
        rptTechnology.DataBind();

    }

    /// <summary>
    /// 列表绑定处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptCase_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataTable dtSort = (DataTable)((Repeater)sender).DataSource;
        Literal ltlInfo = e.Item.FindControl("ltlCaseInfo") as Literal;
        string strInfo = strInfo = Common.NoHTML(dtSort.Rows[e.Item.ItemIndex]["Info"].ToString());
        ltlInfo.Text = (strInfo.Length > 60 ? strInfo.Substring(0, 60) : strInfo) + "…";
    }


    /// <summary>
    /// 列表绑定处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptDoctor_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataTable dtSort = (DataTable)((Repeater)sender).DataSource;
        Literal ltlInfo = e.Item.FindControl("ltlInfo") as Literal;
        string strInfo = strInfo = Common.NoHTML(dtSort.Rows[e.Item.ItemIndex]["Info"].ToString());
        ltlInfo.Text = (strInfo.Length > 12 ? strInfo.Substring(0, 12) : strInfo) + "…";
    }

    ///// <summary>
    ///// 领先技术列表绑定处理
    ///// </summary>
    ///// <param name="sender"></param>
    ///// <param name="e"></param>
    //protected void rptTechnology_ItemDataBound(object sender, RepeaterItemEventArgs e)
    //{
    //    nCount = e.Item.ItemIndex + 2;
    //}

    /// <summary>
    /// 疾病列表绑定处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptDisease_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        nCount++;
        DataTable dtSort = (DataTable)((Repeater)sender).DataSource;
        int nSortID = Convert.ToInt32(dtSort.Rows[e.Item.ItemIndex]["ID"]);
        Literal ltlSubSort = e.Item.FindControl("ltlSubSort") as Literal;
        DataTable dtAllSort = Common.GetDiseaseSort();
        DataRow[] drSubList = dtAllSort.Select("ParentID=" + nSortID.ToString());
        string strHtml = string.Empty;
        string strUrlName = dtSort.Rows[e.Item.ItemIndex]["PageUrlName"].ToString();
        for (int i = 0; i < drSubList.Length; i++)
        {
            strHtml += "<a href=\"/" + strUrlName + "/" + drSubList[i]["PageUrlName"].ToString() + "/\" title=\"" + drSubList[i]["Name"].ToString() + "\" target=\"_blank\">" + Server.HtmlEncode(drSubList[i]["Name"].ToString()) + "</a>  ";
            if (i == 5)
                break;
        }
        ltlSubSort.Text = strHtml;

        DataTable dtArticle = new BisArticle().GetTopArticle(8, ArticelTypeEnum.Disease, nSortID);
        Repeater rptDiseaseInfo = e.Item.FindControl("rptDiseaseInfo") as Repeater;
        rptDiseaseInfo.DataSource = dtArticle;
        rptDiseaseInfo.DataBind();
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string strSearch = string.Empty;
        strSearch = txtSearch.Text.Trim();
        Response.Redirect("/search/?key=" + strSearch);
    }

}